<?php

namespace api\modules\backend\models;

use Yii;
use \yii\db\ActiveRecord;

/**
 * Guide Model
 *
 * @author Fong <xiaofeng.zhang@suppernovachina.com>
 */
class Guide extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%guide}}';
    }

    /**
     * @inheritdoc
     */
    public static function primaryKey()
    {
        return ['guide_id'];
    }

    /**
     * Define rules for validation
     */
    public function rules()
    {
        return [
            [['name', 'account', 'mobile', 'active'], 'required'],
            [['name'], 'string', 'length' => [2, 20]],
            [['account', 'mobile'], 'unique'],
            [['account'], 'match', 'pattern' => '/^(\w){4,20}$/'],
            [['password_hash'], 'required', 'on' => 'create'],
            [['mobile'], 'integer'],
            [['mobile'], 'match', 'pattern' => '/^1[34578]\d{9}$/'],
            [['active'], 'in', 'range' => [0, 1]]
        ];
    }

    /**
     * Generates password hash from password and sets it to the model
     * @param $password
     * @throws \yii\base\Exception
     */
    public function setPassword($password)
    {
        $this->password_hash = Yii::$app->security->generatePasswordHash($password);
    }
}
